13. Representing Data
Representing Data
In this section, you will learn how to create Java classes that represent data
ND079 JPND C2 L02 A14 Representing Data In Java
In many programming contexts, programmers need to be able to represent custom data types. Java is no different.
Example
Suppose you are reading a client's basic information — such as their id, first name, and last name — from a database record.
The class representing a client's data in your system might look like this:
public class Client {
private int id;
private String name;
private List<String> emails;
public int getId() { return id; }
public String getName() { return name; }
public List<String> getEmails() { return emails; }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setEmails(List<String> emails) { this.emails = emails; }
}
Each piece of information is stored in an instance field, with a getter and setter method. The method names are prefixed by "get"
and "set"
, respectively. followed by the name of the property.
A data class that follows this naming convention is called a Java Bean.
SOLUTION:
- `get`
- `set`